package com.thirtyfour;

public class ThirtyFour {

    public int findFirstPosition(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        if (nums.length == 0) return -1;

        while (left <= right) {
            int mid = (left + right) / 2;
            if (target <= nums[mid]) {
                right = mid - 1;
            }
            if (target > nums[mid]) {
                left = mid + 1;
            }
            if (left >= nums.length - 1) return -1;
            if (nums[left] == target) {
                return left;
            }
        }
        return -1;
    }


    public int findLastPosition(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        if (nums.length == 0) return -1;

        while (left <= right) {
            int mid = (left + right) / 2 + 1;


            if (target < nums[mid]) {
                right = mid - 1;
            }
            if (target >= nums[mid]) {
                left = mid + 1;
            }
            if (left >= nums.length - 1) return -1;
            if (nums[right] == target) {
                return right;
            }
        }
        return -1;
    }
}
